using Microsoft.EntityFrameworkCore;
using ZZ_ICU.Models;
using ZZ_ICU.Services.Interfaces;

namespace ZZ_ICU.Services
{
    // 认证服务实现：当前采用明文密码匹配（演示用途）。
    // 可在此集中接入密码哈希、账号锁定、审计日志等能力。
    public class AuthService : IAuthService
    {
        private readonly ApplicationDbContext _dbContext;

        public AuthService(ApplicationDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public async Task<(int userId, string userName, string userNo, int userType)?> ValidateCredentialsAsync(string userNo, string password)
        {
            if (string.IsNullOrWhiteSpace(userNo) || string.IsNullOrWhiteSpace(password))
            {
                return null;
            }

            // 只取必要字段，减小查询负载
            var user = await _dbContext.NurseInfo
                .AsNoTracking()
                .Where(u => u.UserNo == userNo && u.Password == password && u.IsDel == false)
                .Select(u => new { u.Id, u.Name, u.UserNo, u.UserType })
                .FirstOrDefaultAsync();

            if (user == null)
            {
                return null;
            }

            return (user.Id, user.Name, user.UserNo, (user.UserType ?? 0));
        }
    }
}


